package cn.i7baozh.cloud.payment.controller;

import cn.hutool.core.lang.UUID;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Enumeration;

/**
 * @author zhangbaoqi532
 * @date 2024/12/27
 */
@Slf4j
@RestController
public class PayGatewayController {

    @Value("${server.port}")
    private Integer serverPort;

    @GetMapping("/pay/custom/test")
    public String custom(HttpServletRequest request) {
        System.out.println("=======================");

        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String param = parameterNames.nextElement();
            System.out.println("参数:" + param + " -> " + request.getParameter(param));
        }
        System.out.println("-----------------------");
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String header = headerNames.nextElement();
            System.out.println("请求头:" + header + " -> " + request.getHeader(header));
        }
        System.out.println("-----------------------");
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println("cookie:" + cookie.getName() + " -> " + cookie.getValue());
            }
        }
        System.out.println("=======================");

        return "请求成功，服务器[" + serverPort + "] 返回： " + UUID.fastUUID().toString(true);
    }


    @GetMapping("/pay/gateway/{id}")
    public String gateway(@PathVariable(value = "id") Integer id, HttpServletRequest request) {

        System.out.println("=======================");

        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String param = parameterNames.nextElement();
            System.out.println("参数:" + param + " -> " + request.getParameter(param));
        }
        System.out.println("-----------------------");
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String header = headerNames.nextElement();
            System.out.println("请求头:" + header + " -> " + request.getHeader(header));
        }
        System.out.println("-----------------------");
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println("cookie:" + cookie.getName() + " -> " + cookie.getValue());
            }
        }
        System.out.println("=======================");

        return "请求成功，服务器[" + serverPort + "] 返回： " + UUID.fastUUID().toString(true);
    }
}
